﻿using System;
using FootballCourt.Service.MessageContracts;
using FootballCourt.Service.ServiceContracts;
using log4net;
using FootballCourt.Data.Abstract;
using System.Collections.Generic;
using FootballCourt.Domain.Entity;
using FootballCourt.Service.MessagesBases;
using AutoMapper;
using FootballCourt.Service.DataContracts;

namespace FootballCourt.Service.ServiceImpls
{
    public class SystemRoleService : ISystemRoleService
    {
        internal static ILog _logger;
        private IUnitOfWork _unitOfWork;
        private ISystemRoleRepository roleRepository;
        internal static void CreateMapper()
        {
            Mapper.CreateMap<SystemRole, SystemRoleContract>();
            Mapper.CreateMap<SystemRoleContract, SystemRole>();
        }
        public SystemRoleService(IUnitOfWork unitOfWork, ISystemRoleRepository repo)
        {
            this._unitOfWork = unitOfWork;
            this.roleRepository = repo;
            SystemRoleService._logger = log4net.LogManager.GetLogger((System.Reflection.MethodBase.GetCurrentMethod().DeclaringType));
            SystemRoleService.CreateMapper();
        }

        public SystemRoleResponseMessage GetAll(SystemRoleRequestMessage request)
        {
            var rm = new SystemRoleResponseMessage();
            try
            {
                IEnumerable<SystemRole> list = roleRepository.GetAll();
                if (list != null)
                {
                    rm.Status = ResponseStatus.Success;
                    var enumerator = list.GetEnumerator();
                    List<SystemRoleContract> returnList = new List<SystemRoleContract>();
                    while (enumerator.MoveNext())
                    {
                        returnList.Add(Mapper.Map<SystemRoleContract>(enumerator.Current));
                    }
                    rm.roleList = returnList;
                }
                else
                {
                    rm.Status = ResponseStatus.Failure;
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
                rm.Status = ResponseStatus.Failure;
            }
            return rm;
        }
    }
}
